package com.iretailer.report.quartz.sendReqularly.protocol;

import com.iretailer.report.model.SendRegularly;
import com.iretailer.report.quartz.sendReqularly.model.FlowArray;
import com.iretailer.report.quartz.sendReqularly.model.ServerConfig;
import com.iretailer.report.quartz.sendReqularly.util.CommonUtil;
import com.iretailer.report.util.StringUtil;
import com.iretailer.report.util.TimeUtil;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.net.ftp.FTPSClient;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Auther: xjw
 * @Date: 2019/8/1 0001 11:24
 * @Description:
 */
public class FTPSProtocol {


    private String host;
    private int port;
    private String userName;
    private String password;

    public FTPSProtocol(String host, int port, String userName, String password) {
        this.host = host;
        this.port = port;
        this.userName = userName;
        this.password = password;
    }

    public static boolean initAndSend(SendRegularly s, ServerConfig config, String fileName, byte[] message) throws Exception {
        FTPSProtocol ftps = new FTPSProtocol(config.getHost(), config.getPort(), config.getUserName(), config.getPassword());
        return ftps.upload(config.getDirectory(), config.getWorkMode(), fileName, message);
    }

    public boolean upload(String filePath, String workMode, String fileName, byte[] message) throws Exception {
        boolean result = false;
        InputStream is = null;
        FTPSClient ftpsClient = new FTPSClient();
        try {
            ftpsClient.connect(host, port);
            int reply = ftpsClient.getReplyCode();
            if (FTPReply.isPositiveCompletion(reply)) {
                boolean login = ftpsClient.login(userName, password);
                if (!login) {
                    throw new Exception("Account password error");
                }
                boolean flag = ftpsClient.changeWorkingDirectory(filePath);
                if (flag) {
//                    if (FTPReply.isPositiveCompletion(ftpsClient.sendCommand("OPTS UTF-8", "ON"))) {
//                        LOCAL_CHARSET = "UTF-8";
//                    }
                    ftpsClient.setFileType(FTP.LOCAL_FILE_TYPE);
//                    ftpsClient.setControlEncoding(LOCAL_CHARSET);
                    //主动模式还是 被动模式
//                    ftpsClient.enterLocalActiveMode();
                    if ("active".equals(workMode)) {
                        ftpsClient.enterLocalActiveMode();
                    } else {
                        ftpsClient.enterLocalPassiveMode();
                    }
//                    byte[] header = new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF};
//                    byte[] csv_data = message.getBytes(Charset.forName(LOCAL_CHARSET));
//                    ByteArrayOutputStream os = new ByteArrayOutputStream();
//                    os.write(header);
//                    os.write(csv_data);
                    is = new ByteArrayInputStream(message);
//                    os.close();
//                     is = new ByteArrayInputStream(CommonUtil.Json2CSV(message, FlowArray.class).getBytes(Charset.forName(LOCAL_CHARSET)));
                    boolean store = ftpsClient.storeFile(fileName, is);
                    if (store) {
                        result = true;
                    } else {
                        throw new RuntimeException(String.format("store file is fail"));
                    }

                } else {
                    throw new RuntimeException(String.format("ftps server not have this path %s", filePath));
                }
            } else {
                throw new RuntimeException("FTPS connect to host failed");
            }
        } finally {
            if (is != null) {
                is.close();
            }
            ftpsClient.logout();
            ftpsClient.disconnect();
        }
        return result;
    }
}
